#include "Stdafx.h"
#include "RealTimeController.h"

namespace Pulpy
{

	float RealTimeController::_FPSLimit = 30.0f;
	float RealTimeController::_FPS = 0;
	int RealTimeController::_TimeInterval = 0;

	RealTimeController RealTimeController::getInstance()
	{
		static RealTimeController s_instance;
		return s_instance;
	}
	void RealTimeController::setFPSLimit(float limit)
	{
		_FPSLimit = limit;
	}
	float RealTimeController::getFPSLimit()
	{
		return _FPSLimit;
	}
	float RealTimeController::getFPS()
	{
		return _FPS;
	}
	int RealTimeController::getTimeInterval()
	{
		return _TimeInterval;
	}
	bool RealTimeController::isUpdate()
	{
		static int _Frame = 0;
		static long _Last = timeGetTime();
		static int _Prev = 0;
		static int _Cur = 0;
		bool temp = (timeGetTime()-_Last) > 1000/_FPSLimit;
		if(temp)
		{
			_Frame++;
			_Cur = glutGet(GLUT_ELAPSED_TIME);
			_TimeInterval = _Cur - _Prev;
			if(_TimeInterval > 1000)
			{
				_FPS = _Frame / (_TimeInterval / 1000.0f);
				_Prev = _Cur;
				_Frame = 0;
			}
			_Last = timeGetTime();
			//_Frame = 0;
		}			
		return temp;
	}
}